<template>
    <el-container>
        <el-header class="header-tabs">
            <el-tabs type="card" v-model="groupId" @tab-change="tabChange">
                <el-tab-pane label="全部" name="2">
                    <template #label>
                        <el-badge :value="headnumber.total" class="item" :max="9999">
                            <div style="width:40px">全部</div>
                        </el-badge>
                    </template>
                </el-tab-pane>
                <el-tab-pane label="预售中" name="0">
                    <template #label>
                        <el-badge :value="headnumber.unrefund" class="item" :max="9999">
                            <div style="width:50px">预售中</div>
                        </el-badge>
                    </template>
                </el-tab-pane>
                <el-tab-pane label="已退款" name="1">
                    <template #label>
                        <el-badge :value="headnumber.refund" class="item" :max="9999">
                            <div style="width:50px">已退款</div>
                        </el-badge>
                    </template>
                </el-tab-pane>
            </el-tabs>
        </el-header>
        <el-header>
            <div class="left-panel">
                <el-button type="primary" icon="el-icon-plus" @click="add" v-auth="'hotel.add'"></el-button>
                <el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length == 0" @click="batch_del" v-auth="'hotel.delete'"></el-button>
                <!-- <sc-file-import :apiObj="$API.order.entrust.orderUp" @success="success" ref="importBox" templateUrl="https://2156.mzth.cn/import.xlsx"></sc-file-import> -->
            </div>
            <div class="right-panel">
                <div class="right-panel-search" style="flex-wrap:wrap;">
                    <el-input style="margin:5px 10px" v-model="search.project_code" placeholder="请输入项目编号" clearable @keyup.enter="upsearch"></el-input>
                    <el-input style="margin:5px 10px" v-model="search.metting" placeholder="请输入会议名称" clearable @keyup.enter="upsearch"></el-input>
                    <el-input style="margin:5px 10px" v-model="search.banquet_name" placeholder="请输入宴会厅名称" clearable @keyup.enter="upsearch"></el-input>
                    <el-date-picker v-model="search.project_time" :value-format="'YYYY-MM-DD'" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 230px" />
                    <el-select v-model="search.pre_status" placeholder="请选择预售状态" clearable>
                        <el-option label="全部" value="2"/>
                        <el-option label="预售中" value="0"/>
                        <el-option label="已退款" value="1"/>
                    </el-select>
                    <el-button type="primary" icon="el-icon-search" @click="upsearch">搜索</el-button>
                    <el-button type="primary" icon="el-icon-refresh" @click="refresh">重置</el-button>
                </div>
            </div>
        </el-header>
        <el-main class="nopadding">
            <scTable ref="table" row-key="id" :apiObj="apiObj" @selection-change="selectionChange" remoteSort remoteFilter border>
                <el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
                <el-table-column label="项目编号" prop="project.project_code" align="center"></el-table-column>
                <el-table-column label="会议名称" prop="project.metting" align="center"></el-table-column>
                <el-table-column label="宴会厅名称" prop="project.banquet.banquet_name" align="center"></el-table-column>
                <el-table-column label="会议日期" prop="project.project_time" align="center">
                    <!-- <template #default="scope">
                        {{ moment(scope.row.project.project_time * 1000).format('YYYY-MM-DD') }}
                    </template> -->
                </el-table-column>
                <el-table-column label="预售金额" prop="pre_sale" align="center"></el-table-column>
                <el-table-column label="拍照上传" prop="photo_files" align="center">
                    <template #default="scope">
                        <el-image class="img" v-if="scope.row.photo_files" :src="(scope.row.photo_files).split(',')[0]" :preview-src-list="(scope.row.photo_files).split(',')" fit="cover" preview-teleported="true" hide-on-click-modal />
                    </template>
                </el-table-column>
                <el-table-column label="预售状态" prop="pre_status" align="center">
                    <template #default="scope">
                        <el-tag v-if="scope.row.pre_status == 0" type="success">预售中</el-tag>
                        <el-tag v-else-if="scope.row.pre_status == 1" type="danger">已退款</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center">
                    <template #default="scope">
                        <el-button-group>
                            <el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)" v-auth="'hotel.show'">查看</el-button>
                            <el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)" v-auth="'hotel.edit'">编辑</el-button>
                            <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
                                <template #reference>
                                    <el-button text type="danger" size="small" v-auth="'hotel.delete'">删除</el-button>
                                </template>
                            </el-popconfirm>
                        </el-button-group>
                    </template>
                </el-table-column>
            </scTable>
        </el-main>
    </el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false" draggable></save-dialog>
</template>

<script>
import saveDialog from './save.vue'

export default {
	name: '客户管理',
	components: {
		saveDialog,
	},
	data() {
		return {
			dialog: {
				save: false,
			},
			apiObj: this.$API.project.hotel.list,
			selection: [],
			search: {
				project_code: null,
				metting: null,
				project_time: null,
				banquet_name: null,
				pre_status: null,
			},
            groupId:"2",
            headnumber:{},
		}
	},
	mounted() {
        this.getNum()
	},
	methods: {
        success(res){
			if(res.code==1){
				this.$refs.table.refresh()
                this.getNum()
				this.$refs.importBox.close()
				this.$message.success("导入成功")
			} else{
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//添加
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		//查看
		table_show(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('show').setData(row)
			})
		},
		//删除
		async table_del(row, index) {
			var reqData = {id: row.id}
			var res = await this.$API.project.hotel.delete.post(reqData);
			if (res.code == 1) {
				//这里选择刷新整个表格 OR 插入/编辑现有表格数据
				this.$refs.table.tableData.splice(index, 1);
                this.$refs.table.refresh()
                this.getNum()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async table_del_all(delete_ids) {
			var reqData = {id: delete_ids}
			var res = await this.$API.project.hotel.delete.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
                this.getNum()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//批量删除
		async batch_del() {
			var delete_ids = [];
			this.selection.forEach((item) => {
				delete_ids.push(item.id)
			})
			this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(() => {
				this.table_del_all(delete_ids)
			}).catch(() => {

			})
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			}
            this.getNum()
		},
        refresh(){
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.groupId = "2"
            this.$refs.table.upData(this.search)
        },
        tabChange(e){
            this.groupId = e
            if(e==2){
                this.search.pre_status = null
            }else{
                this.search.pre_status = e
            }
            this.upsearch()
        },
        async getNum(){
            var res = await this.$API.project.hotel.list.get();
            this.headnumber = res.data.extend
        },
	}
}
</script>

<style>
.img{
    width: 80px;
    height: 80px;
}
</style>
